package svc

import (
	"context"
	"github.com/zeromicro/go-zero/zrpc"
	"zhongxc.cc/admin-common/tools"
	"zhongxc.cc/admin-user-api/internal/config"
	"zhongxc.cc/grpc-common/admin-user/client/login"
	"zhongxc.cc/grpc-common/admin-user/client/menu"
	"zhongxc.cc/grpc-common/admin-user/client/user"
)

type ServiceContext struct {
	Config   config.Config
	UserRpc  user.User
	LoginRpc login.Login
	MenuRpc  menu.Menu
}

func NewServiceContext(c config.Config) *ServiceContext {
	return &ServiceContext{
		Config:   c,
		UserRpc:  user.NewUser(zrpc.MustNewClient(c.UserRpc)),
		LoginRpc: login.NewLogin(zrpc.MustNewClient(c.UserRpc)),
		MenuRpc:  menu.NewMenu(zrpc.MustNewClient(c.UserRpc)),
	}
}

// GetUserInfo
// @Description: 获取用户信息
// @receiver s
// @param ctx
// @return *tools.AccessDetails
func (s *ServiceContext) GetUserInfo(ctx context.Context) *tools.AccessDetails {
	userInfo := ctx.Value("userinfo")
	if userInfo != nil {
		return userInfo.(*tools.AccessDetails)
	}
	return &tools.AccessDetails{}
}
